/*
 *   Copyright (c) 2010 Sonrisa Informatikai Kft. All Rights Reserved.
 * 
 *  This software is the confidential and proprietary information of
 *  Sonrisa Informatikai Kft. ("Confidential Information").
 *  You shall not disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into
 *  with Sonrisa.
 * 
 *  SONRISA MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 *  THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 *  TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 *  PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SONRISA SHALL NOT BE LIABLE FOR
 *  ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 *  DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package com.zh.jbet.web.panels.context;

import com.zh.jbet.core.entity.Game;
import com.zh.jbet.core.entity.GameContext;
import com.zh.jbet.web.components.datatable.ModalEditCloseListener;
import com.zh.jbet.web.model.GameContextTreeModel;
import com.zh.jbet.web.pages.BasePanel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.tree.BaseTree;
import org.apache.wicket.markup.html.tree.LinkTree;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

/**
 *
 * @author sonrisa
 */
public class ContextTreeEditPanel<T extends Game> extends BasePanel {
    public ContextTreeEditPanel(String id, final IModel<TreeModel> model) {
        super(id, model);
        final WebMarkupContainer showGameCont = new WebMarkupContainer("showGameCont", new CompoundPropertyModel<Game>(model.getObject().getRoot()));
        add(showGameCont);
        showGameCont.setOutputMarkupId(true);
        showGameCont.add(new Label("userObject.name"));
        showGameCont.add(new Label("userObject.shortName"));
        final LinkTree ltree = new LinkTree("tree", model) {
            @Override
            protected IModel getNodeTextModel(IModel<Object> tmodel) {
                return new PropertyModel(tmodel, "userObject.name");
            }

            @Override
            protected void onNodeLinkClicked(Object onode, BaseTree btree, AjaxRequestTarget target) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) onode;
                showGameCont.setDefaultModelObject(node);
                btree.getTreeState().selectNode(onode, true);
                target.addComponent(showGameCont);
                target.addComponent(getFeedback());
            }

        };
        ltree.setOutputMarkupId(true);
        ltree.getTreeState().expandAll();
        ltree.getTreeState().setAllowSelectMultiple(false);
        ltree.getTreeState().selectNode(model.getObject().getRoot(), true);
        add(ltree);
        add(new AjaxLink("addContext") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                editNode(ltree, target, false);
            }
        });

        add(new AjaxLink("editContext") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                editNode(ltree, target, true);
            }
        });

        add(new AjaxLink("saveTree") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                saveAndBack(target, true);
            }
        });
        add(new AjaxLink("reloadTree") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                getContextTreeModel().reload();
                ltree.getTreeState().selectNode(model.getObject().getRoot(), true);
                showGameCont.setDefaultModelObject(model.getObject().getRoot());
                target.addComponent(showGameCont);
                target.addComponent(ltree);
                target.addComponent(getFeedback());
            }
        });
    }

    public GameContextTreeModel getContextTreeModel() {
        return (GameContextTreeModel)getDefaultModel();
    }

    public DefaultTreeModel getContextTreeModelObject() {
        return (DefaultTreeModel)getDefaultModelObject();
    }

    private void saveAndBack(AjaxRequestTarget target, boolean save) {
        getContextTreeModel().save();
/*
        GameService<T> service = provider.getGameService();
        for (DefaultMutableTreeNode node: markToDelete.getObject()) {
            service.deleteContext(node);
        }
        if (save) {
            service.saveGameContext((DefaultMutableTreeNode)treeModel.getRoot());
        }
 * 
 */
        getJBetPage().backToParentPanel(target);
    }

    private void editNode(final LinkTree ltree, AjaxRequestTarget target, final boolean isedit) {
        final DefaultMutableTreeNode selected = (DefaultMutableTreeNode)ltree.getTreeState().getSelectedNodes().iterator().next();
        final GameContext gc = isedit ? (GameContext)selected.getUserObject() : new GameContext();
        final ContextEditModalPanel gcm = new ContextEditModalPanel(getJBetPage().getModal(), new Model<GameContext>(gc), isedit);
        gcm.setEditCloseListener(new ModalEditCloseListener() {
            @Override
            public void onModalClose(AjaxRequestTarget target, boolean save, boolean delete) {
                if (save) {
                    if (!isedit) {
                        DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(gcm.getDefaultModelObject());
                        getContextTreeModelObject().insertNodeInto(newNode, selected, selected.getChildCount());
                    } else {
                        selected.setUserObject(gcm.getDefaultModelObject());
                    }
                    target.addComponent(ltree);
                }
                if (delete) {
                    getContextTreeModel().markToDelete(selected);
                    if (selected == getContextTreeModel().getObject().getRoot()) {
                        saveAndBack(target, false);
                    } else {
                        ltree.getTreeState().selectNode(selected.getParent(), true);
                        getContextTreeModelObject().removeNodeFromParent(selected);
                        target.addComponent(ltree);
                    }
                }
            }
        });
        gcm.show(target);
    }
    /*
    public ShowContextPanel createEditPanel(final BaseTree tree, final DefaultMutableTreeNode node) {
        return new ShowContextPanel(EDIT_PANEL, node) {
            @Override
            protected void onPanelSubmit(AjaxRequestTarget target) {
                node.setUserObject(getDefaultModelObject());
                target.addComponent(tree);
            }
        };
    }
     *
     */
}
